<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="88px">
			<el-form-item label="档案区编号" prop="areaNo">
				<el-tree-select  @change="changeArea" v-model="queryParams.areaNo" :data="areaOptions"
					:props="{ value: 'archAreaNo', label: 'label', children: 'children' }" value-key="archAreaNo"
					placeholder="请选择档案区" clearable @keyup.enter="handleQuery" />
			</el-form-item>
			<el-form-item label="档案柜" prop="cabinetNo">
				<el-tree-select v-model="queryParams.cabinetNo" :data="cabinetOptions"
					:props="{ value: 'colNo', label: 'label', children: 'children' }" value-key="colNo"
					placeholder="请选择档案柜" clearable @keyup.enter="handleQuery" @click="getListArchiveCabinet"/>
			</el-form-item>
			<el-form-item label="档案盒号" prop="boxNo">
				<el-input v-model="queryParams.boxNo" placeholder="请输入档案盒号" clearable
					@keyup.enter="handleQuery" />
			</el-form-item>
			<el-form-item label="档案名称" prop="archiveName">
				<el-input v-model="queryParams.archiveName" placeholder="请输入档案名称" clearable
					@keyup.enter="handleQuery" />
			</el-form-item>
			<el-form-item label="档案类型" prop="archiveType">
				<el-select v-model="queryParams.archiveType" placeholder="请选择档案类型">
					<el-option v-for="dict in archive_type" :key="dict.value" :label="dict.label" :value="dict.value">
					</el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="资料种类" prop="meansType">
				<el-select v-model="queryParams.meansType" placeholder="请选择资料种类">
					<el-option v-for="dict in means_type" :key="dict.value" :label="dict.label" :value="dict.value">
					</el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="保管期限" prop="saveDate">
				<el-select v-model="queryParams.saveDate" placeholder="请选择保管期限">
					<el-option v-for="dict in keep_dur" :key="dict.value" :label="dict.label" :value="dict.value">
					</el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="档案密级" prop="conLevel">
				<el-select v-model="queryParams.conLevel" placeholder="请选择档案密级">
					<el-option v-for="dict in confd_lv" :key="dict.value" :label="dict.label" :value="dict.value">
					</el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="介质类型" prop="medForm">
				<el-select v-model="queryParams.medForm" placeholder="请选择介质类型">
					<el-option v-for="dict in med_form" :key="dict.value" :label="dict.label" :value="dict.value">
					</el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="对象编号" prop="archObjNo">
				<el-input v-model="queryParams.archObjNo" placeholder="请输入对象编号" clearable @keyup.enter="handleQuery" />
			</el-form-item>
			<el-form-item label="对象名称" prop="archObjName">
				<el-input v-model="queryParams.archObjName" placeholder="请输入对象名称" clearable
					@keyup.enter="handleQuery" />
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
				<el-button icon="Refresh" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<!-- <el-col :span="1.5">
				<el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['archive:roomArchive:add']">新增</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate" v-hasPermi="['archive:roomArchive:edit']">修改</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete" v-hasPermi="['archive:roomArchive:remove']">删除</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['archive:roomArchive:export']">导出</el-button>
			</el-col> -->
			<right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>

		<el-table v-loading="loading" :data="roomArchiveList" @selection-change="handleSelectionChange">
			<!-- <el-table-column type="selection" width="55" align="center" /> -->
			<el-table-column label="序号" align='center' width="80">
				<template #default="scope">
					{{ scope.$index+1 }}
				</template>
			</el-table-column>
			<el-table-column label="档案区编号" width="150" align="center" prop="areaNo" />
			<el-table-column label="档案柜编号" align="center" prop="cabinetNo" width="150" />
			<el-table-column label="档案盒号" align="center" prop="boxNo" width="150" />
			<el-table-column label="借阅状态" align="center" prop="giveBackStatus">
				<template #default="scope">
					<dict-tag :options="borrow_flag" :value="scope.row.giveBackStatus" />
				</template>
			</el-table-column>
			<el-table-column label="档案名称" align="center" prop="archiveName" width="330" />
			<el-table-column label="档案类型" align="center" prop="archiveType" width="150">
				<template #default="scope">
					<dict-tag :options="archive_type" :value="scope.row.archiveType" />
				</template>
			</el-table-column>
			<el-table-column label="档案密级" align="center" prop="conLevel" width="100">
				<template #default="scope">
					<dict-tag :options="confd_lv" :value="scope.row.conLevel" />
				</template>
			</el-table-column>
			<el-table-column label="保管期限" align="center" prop="saveDate" width="100">
				<template #default="scope">
					<dict-tag :options="keep_dur" :value="scope.row.saveDate" />
				</template>
			</el-table-column>

			<!-- <el-table-column label="保管情况" align="center" prop="saveStatus" width="100">
				<template #default="scope">
					<dict-tag :options="save_extended" :value="scope.row.saveStatus" />
				</template>
			</el-table-column> -->
			<el-table-column label="资料种类	" align="center" prop="meansType" width="150">
				<template #default="scope">
					<dict-tag :options="means_type" :value="scope.row.meansType" />
				</template>
			</el-table-column>
			<el-table-column label="介质类型" align="center" prop="medForm">
				<template #default="scope">
					<dict-tag :options="med_form" :value="scope.row.medForm" />
				</template>
			</el-table-column>
			<el-table-column label="对象类型" align="center" prop="archObjType">
				<template #default="scope">
					<dict-tag :options="arch_obj_type" :value="scope.row.archObjType" />
				</template>
			</el-table-column>
			<el-table-column label="对象编号" align="center" prop="archObjNo" width="150" />
			<el-table-column label="对象名称" align="center" prop="archObjName" width="150" />
			<el-table-column label="操作" align="center" fixed="right" width="150">
				<template #default="scope">
					<el-button type="text" icon="View" @click="handleSee(scope.row)"
						v-hasPermi="['archive:roomArchive:edit']">查看详情</el-button>
					<!-- <el-button type="text" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['archive:roomArchive:edit']">查看详情</el-button> -->
					<!-- <el-button type="text" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['archive:roomArchive:remove']">删除</el-button> -->
				</template>
			</el-table-column>
		</el-table>

		<pagination v-show="total>0" :total="total" v-model:page="queryParams.pageNum"
			v-model:limit="queryParams.pageSize" @pagination="getList" />

		<!-- 添加或修改盒中档案信息对话框 -->
		<el-dialog :title="title" v-model="open" width="500px" append-to-body>
			<el-form ref="roomArchiveRef" :model="form" :rules="rules" label-width="120px">
				<el-form-item label="档案区编号" prop="areaNo">
					<el-select v-model="form.areaNo" placeholder="请选择档案区编号" style="width:230px;" @change="changeArea">
						<el-option v-for="item in areaOptions" :key="item.archAreaNo" :label="item.archAreaNo"
							:value="item.archAreaNo"></el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="档案柜编号" prop="cabinetNo">
					<el-select v-model="form.cabinetNo" placeholder="请选择档案柜编号" style="width:230px;">
						<el-option v-for="item in cabinetOptions" :key="item.colNo" :label="item.colNo"
							:value="item.colNo"></el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="档案盒号" prop="boxNo">
					<el-select v-model="form.boxNo" placeholder="请选择档案盒号">
						<el-option v-for="item in boxOptions" :key="item.boxCode" :label="item.boxName"
							:value="item.boxCode" style="width:250px;"></el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="档案名称" prop="archiveName">
					<el-input v-model="form.archiveName" placeholder="请输入档案名称" style="width:250px;" />
				</el-form-item>
				<el-form-item label="档案类型" prop="archiveType">
					<el-select v-model="form.archiveType" placeholder="请选择档案类型">
						<el-option v-for="dict in archive_type" :key="dict.value" :label="dict.label"
							:value="dict.value"></el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="档案密级" prop="conLevel">
					<el-select v-model="form.conLevel" placeholder="请选择档案密级">
						<el-option v-for="dict in secrecy_level" :key="dict.value" :label="dict.label"
							:value="dict.value"></el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="保管期限" prop="saveDate">
					<el-select v-model="form.saveDate" placeholder="请选择保管期限">
						<el-option v-for="dict in save_year" :key="dict.value" :label="dict.label" :value="dict.value">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="保管情况" prop="saveStatus">
					<el-select v-model="form.saveStatus" placeholder="请选择保管情况">
						<el-option v-for="dict in save_extended" :key="dict.value" :label="dict.label"
							:value="dict.value"></el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="档案袋标识" prop="bagId">
					<el-input v-model="form.bagId" placeholder="请选择档案袋标识" />
				</el-form-item>
				<el-form-item label="介质类型" prop="medForm">
					<el-input v-model="form.medForm" placeholder="请选择介质类型" />
				</el-form-item>
				<el-form-item label="对象编号" prop="archObjNo">
					<el-input v-model="form.archObjNo" placeholder="请输入对象编号" />
				</el-form-item>
				<el-form-item label="对象名称" prop="archObjName">
					<el-input v-model="form.archObjName" placeholder="请输入对象名称" />
				</el-form-item>
				<!-- <el-form-item label="附件标识" prop="archFileId">
					<el-input v-model="form.archFileId" placeholder="请输入附件标识" />
				</el-form-item> -->
			</el-form>
			<template #footer>
				<div class="dialog-footer">
					<el-button type="primary" @click="submitForm">确 定</el-button>
					<el-button @click="cancel">取 消</el-button>
				</div>
			</template>
		</el-dialog>
		<!-- 查看详情 -->
		<el-dialog :title="titleSee" v-model="openSee" width="500px" append-to-body>
			<el-form ref="roomArchiveRef" :model="formSee" :rules="rules" label-width="120px">
				<el-form-item label="档案区编号" prop="areaNo">
					<el-select v-model="formSee.areaNo" placeholder="请选择档案区编号" style="width:230px;" disabled="disabled">
						<el-option v-for="item in areaOptions" :key="item.archAreaNo" :label="item.archAreaNo"
							:value="item.archAreaNo"></el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="档案柜编号" prop="cabinetNo">
					<el-select v-model="formSee.cabinetNo" placeholder="请选择档案柜编号" style="width:230px;"
						disabled="disabled">
						<el-option v-for="item in cabinetOptions" :key="item.colNo" :label="item.colNo"
							:value="item.colNo"></el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="档案盒号" prop="boxNo">
					<el-select v-model="formSee.boxNo" placeholder="请选择档案盒号" disabled="disabled">
						<el-option v-for="item in boxOptions" :key="item.boxCode" :label="item.boxName"
							:value="item.boxCode" style="width:250px;"></el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="档案名称" prop="archiveName">
					<el-input v-model="formSee.archiveName" placeholder="请输入档案名称" style="width:250px;"
						disabled="disabled" />
				</el-form-item>
				<el-form-item label="档案类型" prop="archiveType">
					<el-select v-model="formSee.archiveType" placeholder="请选择档案类型" disabled="disabled">
						<el-option v-for="dict in archive_type" :key="dict.value" :label="dict.label"
							:value="dict.value"></el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="档案密级" prop="conLevel">
					<el-select v-model="formSee.conLevel" placeholder="请选择档案密级" disabled="disabled">
						<el-option v-for="dict in confd_lv" :key="dict.value" :label="dict.label" :value="dict.value">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="保管期限" prop="saveDate">
					<el-select v-model="formSee.saveDate" placeholder="请选择保管期限" disabled="disabled">
						<el-option v-for="dict in keep_dur" :key="dict.value" :label="dict.label" :value="dict.value">
						</el-option>
					</el-select>
				</el-form-item>
				<!-- <el-form-item label="档案袋标识" prop="bagId">
					<el-input v-model="formSee.bagId" placeholder="请选择档案袋标识" />
				</el-form-item> -->
				<el-form-item label="介质类型" prop="medForm">
					<el-select v-model="formSee.medForm" placeholder="请选择介质类型" disabled="disabled">
						<el-option v-for="dict in med_form" :key="dict.value" :label="dict.label" :value="dict.value">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="对象编号" prop="archObjNo">
					<el-input v-model="formSee.archObjNo" placeholder="请输入对象编号" disabled="disabled" />
				</el-form-item>
				<el-form-item label="对象名称" prop="archObjName">
					<el-input v-model="formSee.archObjName" placeholder="请输入对象名称" disabled="disabled" />
				</el-form-item>
			</el-form>

			<template #footer>
				<div class="dialog-footer">
					<el-button @click="cancel">返 回</el-button>
				</div>
			</template>
		</el-dialog>
	</div>
</template>

<script setup name="RoomArchive">
	import {
		listRoomArchive,
		getRoomArchive,
		delRoomArchive,
		addRoomArchive,
		updateRoomArchive
	} from "@/api/archive/roomArchive";
	import {
		getCurrentInstance,
		reactive,
		ref,
		toRefs
	} from "vue";
	import {
		listBox
	} from "@/api/base/box";
	import {
		listArchiveCabinetAll
	} from "@/api/archive/ArchiveCabinet";
	import {
		listArchiveArchAreaAll
	} from "@/api/archive/ArchiveArchArea";
	const {
		proxy
	} = getCurrentInstance();

	const roomArchiveList = ref([]);
	const open = ref(false);
	const openSee = ref(false);
	const loading = ref(true);
	const showSearch = ref(true);
	const ids = ref([]);
	const single = ref(true);
	const multiple = ref(true);
	const total = ref(0);
	const title = ref("");
	const titleSee = ref("");
	const areaOptions = ref([]);
	const cabinetOptions = ref([]);

	const data = reactive({
		form: {

		},
		formSee: {

		},
		queryParams: {
			pageNum: 1,
			pageSize: 10,
			cabinetNo: null,
			boxNo: null,
			archiveType: null,
			archiveName: null,
			saveStatus: null,
			saveDate: null,
			conLevel: null,
			creator: null,
			modifyTime: null,
			modifier: null,
			bagId: null,
			meansType: null,
			medForm: null,
			archObjType: null,
			archObjNo: null,
			archObjName: null,
			archFileId: null,
			areaNo: null
		},
		rules: {
			areaNo: [{
				required: true,
				message: "档案区编号不能为空",
				trigger: "blur"
			}],
			cabinetNo: [{
				required: true,
				message: "档案柜编号不能为空",
				trigger: "blur"
			}],
			boxNo: [{
				required: true,
				message: "档案盒号不能为空",
				trigger: "blur"
			}],
			archiveName: [{
				required: true,
				message: "档案名称不能为空",
				trigger: "blur"
			}],
			archiveType: [{
				required: true,
				message: "档案类型不能为空",
				trigger: "blur"
			}],
		},
		boxqueryParams: {
			pageNum: 1,
			pageSize: 1111111,
			boxCode: null,
			boxName: null,
			boxRfid: null,
			storehouseId: null,
			shelvesId: null,
		},
	});

	const {
		queryParams,
		form,
		formSee,
		rules,
		boxqueryParams
	} = toRefs(data);
	const {
		push_status,
		save_year,
		secrecy_level,
		archive_type,
		save_extended,
		borrow_flag,
		keep_dur,
		confd_lv,
		med_form,
		means_type,
		arch_obj_type
	} = proxy.useDict('push_status', 'save_year', 'secrecy_level', 'archive_type', 'save_extended', 'borrow_flag',
		'keep_dur', 'confd_lv', 'med_form', 'means_type', 'arch_obj_type');
	const boxOptions = ref([]);

	/** 查询盒中档案信息列表 */
	function getList() {
		loading.value = true;
		listRoomArchive(queryParams.value).then(response => {
			roomArchiveList.value = response.rows;
			total.value = response.total;
			loading.value = false;
		});
	}

	// 取消按钮
	function cancel() {
		open.value = false;
		openSee.value = false;
		reset();
	}

	// 表单重置
	function reset() {
		form.value = {
			id: null,
			cabinetNo: null,
			boxNo: null,
			archiveType: null,
			archiveName: null,
			saveStatus: "2",
			saveDate: null,
			conLevel: null,
			createTime: null,
			creator: null,
			modifyTime: null,
			modifier: null,
			bagId: null,
			meansType: null,
			medForm: null,
			archObjType: null,
			archObjNo: null,
			archObjName: null,
			archFileId: null
		};
		formSee.value = {
			id: null,
			cabinetNo: null,
			boxNo: null,
			archiveType: null,
			archiveName: null,
			saveStatus: "2",
			saveDate: null,
			conLevel: null,
			createTime: null,
			creator: null,
			modifyTime: null,
			modifier: null,
			bagId: null,
			meansType: null,
			medForm: null,
			archObjType: null,
			archObjNo: null,
			archObjName: null,
			archFileId: null
		};
		proxy.resetForm("roomArchiveRef");
		boxOptions.value = [];
		areaOptions.value = [];
		cabinetOptions.value = [];
	}

	/** 搜索按钮操作 */
	function handleQuery() {
		queryParams.value.pageNum = 1;
		getList();
	}

	/** 重置按钮操作 */
	function resetQuery() {
		proxy.resetForm("queryRef");
		handleQuery();
	}

	// 多选框选中数据
	function handleSelectionChange(selection) {
		ids.value = selection.map(item => item.id);
		single.value = selection.length != 1;
		multiple.value = !selection.length;
	}

	/** 新增按钮操作 */
	function handleAdd() {
		reset();
		open.value = true;
		initAreaOptions();
		getlistBox();
		title.value = "添加盒中档案信息";
	}

	/** 修改按钮操作 */
	function handleUpdate(row) {
		reset();
		const id = row.id || ids.value;
		getlistBox();
		initAreaOptions();
		getRoomArchive(id).then(response => {
			form.value = response.data;
			open.value = true;
			title.value = "修改盒中档案信息";
		});
	}
	/** 查看详情按钮操作 */
	function handleSee(row) {
		reset();
		const id = row.id || ids.value;
		getlistBox();
		initAreaOptions();
		getRoomArchive(id).then(response => {
			formSee.value = response.data;
			openSee.value = true;
			titleSee.value = "查看详情";
		});
	}

	/** 提交按钮 */
	function submitForm() {
		proxy.$refs["roomArchiveRef"].validate(valid => {
			if (valid) {
				if (form.value.id != null) {
					updateRoomArchive(form.value).then(response => {
						proxy.$modal.msgSuccess("修改成功");
						open.value = false;
						getList();
					});
				} else {
					addRoomArchive(form.value).then(response => {
						proxy.$modal.msgSuccess("新增成功");
						open.value = false;
						getList();
					});
				}
			}
		});
	}

	/** 删除按钮操作 */
	function handleDelete(row) {
		const id = row.id || ids.value;
		proxy.$modal.confirm('是否确认删除盒中档案信息编号为"' + id + '"的数据项？').then(function() {
			return delRoomArchive(id);
		}).then(() => {
			getList();
			proxy.$modal.msgSuccess("删除成功");
		}).catch(() => {});
	}

	/** 导出按钮操作 */
	function handleExport() {
		proxy.download('archive/roomArchive/export', {
			...queryParams.value
		}, `档案信息_${new Date().getTime()}.xlsx`)
	}
	//加载档案盒
	function getlistBox() {
		listBox(boxqueryParams.value).then(response => {
			boxOptions.value = response.rows;
		});
	}

	//根据档案区查询档案柜
	function changeArea() {
		cabinetOptions.value = [];
		form.value.cabinetNo = null;
		queryParams.value.cabinetNo = null;
	}
	/** 查询档案区下拉树结构 */
	function getListArchiveArchArea() {
		listArchiveArchAreaAll().then(response => {
			areaOptions.value = response.rows;
		});
	};

	/** 查询档案柜下拉树结构 */
	function getListArchiveCabinet(queryq) {
		const query = {
			archiveAreaNo: form.value.areaNo || queryParams.value.areaNo
		}
		cabinetOptions.value = [];
		// 检查查询条件是否为空
		if (!query || !query.archiveAreaNo) {
			proxy.$modal.msgError("请选择档案区编号！");
			return;
		}

		listArchiveCabinetAll(query).then(response => {
			cabinetOptions.value = response.rows;
		});
	};

	/** 初始化档案区数据 */
	function initAreaOptions() {
		// 判断档案区的数据是否存在，存在不获取，不存在则获取
		if (areaOptions.value.length === 0) {
			listArchiveArchAreaAll().then(response => {
				areaOptions.value = response.rows;
			});
		}
	};

	initAreaOptions();
	getList();
	getlistBox()
</script>
